class Trie:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = {}
        self.hasEmpty = False


    def insert(self, word: str) -> None:
        """
        Inserts a word into the trie.
        """
        wlen = len(word)
        if wlen == 0:
            self.hasEmpty = True
            return

        curr = self.root
        for index, letter in enumerate(word):
            if letter in curr:
                if index == wlen - 1:
                    curr[letter][1] = True
                    return
                else:
                    curr = curr[letter][0]
            else:
                node = [{}, index == wlen - 1]
                curr[letter] = node
                curr = node[0]

    def search(self, word: str) -> bool:
        """
        Returns if the word is in the trie.
        """
        wlen = len(word)
        if wlen == 0:
            return self.hasEmpty

        curr = self.root
        for index, letter in enumerate(word):
            if letter in curr:
                if curr[letter][1] and index == wlen - 1:
                    return True
                curr = curr[letter][0]
            else:
                return False
        return False



    def startsWith(self, prefix: str) -> bool:
        """
        Returns if there is any word in the trie that starts with the given prefix.
        """
        plen = len(prefix)
        if plen == 0:
            return self.hasEmpty

        curr = self.root
        for index, letter in enumerate(prefix):
            if letter not in curr:
                return False
            curr = curr[letter][0]
        return True



# Your Trie object will be instantiated and called as such:
trie = Trie()

trie.insert("apple");
print(trie.search("apple"))   # 返回 true
print(trie.search("app"))     # 返回 false
print(trie.startsWith("app")) # 返回 true
trie.insert("app");
print(trie.search("app"))     # 返回 true
print(trie.search("appi"))    # 返回 false
print(trie.search("applee"))  # 返回 false
print(trie.startsWith("appi")) # 返回 false
print(trie.startsWith("applee")) # 返回 false
